<?php
/**
 * Here're your description about this file and its function
 *
 * @version			$Id: Bootstrap.php April 11, 2011 11:04:44 PM$
 * @category		ZFAdmin
 * @package			ZFAdmin Package
 * @subpackage		subpackage
 * @copyright		Copyright (C) 2005 - 2011 Open Source Matters. All rights reserved.
 * @author			zendteam@vinamedia.com <Vina Media>
 * @implements		all members
 * @file			Bootstrap.php
 *
 */
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
	protected $_frontController;
	
	/**
	 * Bootstrap autoloader for application resources
	 *
	 * @return Zend_Application_Module_Autoloader
	 */
	protected function _initAutoload ()
	{
		// Ensure front controller instance is present
		$this->bootstrap('frontController');
		$this->_frontController = $this->getResource('frontController');
		$this->_frontController->throwExceptions(FALSE);
		// Add autoloader empty namespace
		$moduleLoader = new Zend_Application_Module_Autoloader(array('namespace' => '', 'basePath' => APP_PATH));
	}

	/**
	 * Setup Database
	 *
	 * @return void
	 */
	protected function _initDB()
	{
		$dbConfig = new Zend_Config_Ini(APP_PATH . DS . 'configs' . DS . 'database.ini');
		$db = Zend_Db::factory($dbConfig->resources->database);
		Zend_Db_Table_Abstract::setDefaultAdapter($db);		
		Zend_Registry::set('db', $db);
	}

	/**
	 * Setup logging
	 *
	 * @return void
	 */
	protected function _initHelpers ()
	{
	}
	/**
	 * Setup View
	 *
	 * @return void
	 */
	protected function _initView ()
	{
		$view = new Zend_View();
		// Instantiate and add the helper in one go
		$viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('ViewRenderer');
		$viewRenderer->setView($view);
		$viewRenderer->setViewSuffix('phtml');
		// Initialise Zend_Layout's MVC helpers
		// by default with layout config option in ../configs/application.ini
		return $view;
	}
	/**
	 * @Desc : Initial all viewer setting here
	 *
	 * @return : void
	 */
	protected function _initViewSettings ()
	{
		try {
			$config = Zend_Registry::get('configuration');
		} catch (Exception $e) {
			$this->_initConfig();
			$config = Zend_Registry::get('configuration');
		}
		$this->bootstrap('view');		
	}
	/**
	 * Setup Plugins
	 *
	 * @return void
	 */
	protected function _initPlugins ()
	{
		$front = Zend_Controller_Front::getInstance();
		$front->registerPlugin(new Core_Controller_Plugin_Route());
	}
		
	/**
	 * Add Controller Action Helpers
	 */
	protected function _initActionHelpers ()
	{
		Zend_Controller_Action_HelperBroker::addPath(
				'Core' . DS . 'Controller' . DS . 'Action' . DS . 'Helper', 'Core_Controller_Action_Helper'
		);
	}
	/**
	 * Setup logging
	 *
	 * @return void
	 */
	protected function _initLogs ()
	{
		$this->bootstrap('log');
		$logs = $this->getPluginResource('log');
		Zend_Registry::set('logger', $logs->getLog()); 
	}

	protected function _initZFDebug()
	{
		$autoloader = Zend_Loader_Autoloader::getInstance();
		$autoloader->registerNamespace('ZFDebug');

		 $options = array(
				'plugins' => array('Variables',
						'File' => array('base_path' => BASE_PATH),
						'Memory',
						'Time',
						'Registry',
						'Exception',
						),	
		 		'jquery_path' => null	 		
		);
		
		$options['plugins']['Database']['adapter'] = Zend_Registry::get('db');
		
		# Setup the cache plugin
		if ($this->hasPluginResource('cache')) {
			$this->bootstrap('cache');
			$cache = $this-getPluginResource('cache')->getDbAdapter();
			$options['plugins']['Cache']['backend'] = $cache->getBackend();
		}

		$debug = new ZFDebug_Controller_Plugin_Debug($options);

		$this->bootstrap('frontController');
		$frontController = $this->getResource('frontController');
		$frontController->registerPlugin($debug); 
	}
	/************************************************************
	 * Here I'm loading my configuration file
	* and i am placing the returned object inside Zend registry
	* this is a bit like having it as a global variable
	************************************************************/
	protected function _initConfig ()
	{
		$configuration = new Zend_Config_Ini(
				APP_PATH . DS . 'configs' . DS . 'configuration.ini', 'bootstrap',
				array(
						'skipExtends' => false,
						'allowModifications' => true)
		);
		Zend_Registry::set('configuration', $configuration);
	}
	/**
	 * Run the application
	 *
	 * Checks to see that we have a default controller directory. If not, an
	 * exception is thrown.
	 *
	 * If so, it registers the bootstrap with the 'bootstrap' parameter of
	 * the front controller, and dispatches the front controller.
	 *
	 * @return void
	 * @throws Zend_Application_Bootstrap_Exception
	 */
	public function run ()
	{
		try {
			$this->_frontController->dispatch();
		} catch (Exception $e) {
			//$logger = Zend_Registry::get('logger');
			$request = $this->_frontController->getRequest();
			$params = $request->getParams();
			unset($params['error_handler']);
			$message = $e->getMessage() . "\n" . $e->getTraceAsString()
			. "\n Param: \n" . var_export($params, true) . "\n";
			$configuration = Zend_Registry::get('configuration');
			if ($configuration->log->write->file == 1) {
				//$logger->log($message, Zend_Log::DEBUG);
			} else {
				echo '<pre>';
				print_R($message);
				echo '</pre>';
				//exit();
			}
		}
	}
}